﻿Imports System.IO

Module ModuleMain

    ''' <summary>
    ''' 主函数
    ''' </summary>
    ''' <remarks></remarks>
    Sub Main()

        Dim Args As String() = System.Environment.GetCommandLineArgs()

        If Args.Count <> 4 Then
            PrintUsage()
            Pause()
            Exit Sub
        End If

        If (Args(1) <> "-e" And Args(1) <> "--encode" And _
             Args(1) <> "-d" And Args(1) <> "--decode") Then
            PrintUsage()
            Pause()
            Exit Sub
        End If

        If Not File.Exists(Args(2)) Then
            Console.WriteLine("文件：{0} 不存在", Args(2))
        End If

        If File.Exists(Args(3)) Then
            Console.Write("文件：{0} 已存在，要覆盖吗 (y/n)", Args(3))
            While True
                Dim temp As String = Console.ReadLine
                If temp = "Y" Or temp = "y" Then
                    Exit While
                ElseIf temp = "N" Or temp = "n" Then
                    Exit Sub
                Else
                    Console.Write("文件：{0} 已存在，要覆盖吗 (y/n)", Args(3))
                    Continue While
                End If
            End While
        End If

        If Args(1) = "-e" Or Args(1) = "--encode" Then
            FileConverHelper.Encode(Args(2), Args(3))
        ElseIf Args(1) = "-d" Or Args(1) = "--decode" Then
            FileConverHelper.Decode(Args(2), Args(3))
        End If

        Console.WriteLine("转换完毕！")
        Console.WriteLine("新生成的文件已被保存到： {0}", Args(3))

        Pause()

    End Sub

    ''' <summary>
    ''' 打印程序用法
    ''' </summary>
    ''' <remarks></remarks>
    Sub PrintUsage()

        Dim Usage(6) As String
        Usage(0) = "Aquilia程序使用方法"
        Usage(1) = "Aquilia [-e|--encode|-e|--decode] 文件1 文件2"
        Usage(2) = "第二个参数为-e或--encode时，Aquilia将文件1加密后写入到文件2中"
        Usage(3) = "第二个参数为-d或--decode时，Aquilia将文件1解密后写入到文件2中"
        Usage(4) = "示例参数1： -e HelloWorld.exe output.txt"
        Usage(5) = "示例参数2： -d output.txt HelloWorld2.exe"
        Console.WriteLine(Join(Usage, vbCrLf))

    End Sub

    ''' <summary>
    ''' 按任意键继续
    ''' </summary>
    ''' <remarks></remarks>
    Sub Pause()

        Console.WriteLine("按任意键继续")
        System.Console.ReadKey()
        System.Console.Write(Chr(8) + " ") '删除按下的“任意键”字符

    End Sub

End Module
